package com.pan.handle.req.impl;

import com.pan.entity.HumbleHttpRequest;
import com.pan.handle.req.RequestHandle;
import com.pan.handle.req.strategy.TestStrategy;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class HumbleHttpReqHandle3 implements RequestHandle {
    @Override
    public HumbleHttpRequest receive(Socket socket) {
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        TestStrategy strategy = new TestStrategy();
        byte[] buf = new byte[4096];
        while (true) {
            int len = 0;
            try {
                len = inputStream.read(buf);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            strategy.appendBuffer(buf, 0, len);
            if (strategy.isComplete()) {
                return strategy.getResult();
            }
        }
    }
}
